#import "CustomMoviePlayerViewController.h"
#import "BdTv.h"
#import "config.h"
#import "AppDelegate.h"



@interface CustomMoviePlayerViewController ()
-(void)btnOnClik:(UIButton*)sender;

//准备播放
- (void)readyPlayer;

@end

@implementation CustomMoviePlayerViewController

@synthesize movieURL;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self view] setBackgroundColor:[UIColor blackColor]];
    
    
    
    
    BdTV *titleView = [[BdTV  alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];;
    [[titleView backBtn]addTarget:self action:@selector(btnOnClik:) forControlEvents:UIControlEventTouchUpInside];
    [[titleView backBtn]setTag:11];
    [[titleView doneBtn]removeFromSuperview];
    [titleView setBackgroundColor:[UIColor blackColor]];
    [[titleView backBtn ]setTitle:@"返回" forState:UIControlStateNormal];
    [[titleView backBtn] setImage:nil forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:titleView];
    
    
    loadingAni = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(PMW/2-38/2, PMH/2-38/2 , 38, 38)];
    loadingAni.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [loadingAni startAnimating];

    
    [self upPlayViewFrame];
    
    
    [self readyPlayer];
    
}



- (void) readyPlayer
{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayerLoadStateChanged:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeFrames:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    
    
    mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    [mp setControlStyle:MPMovieControlStyleFullscreen];
    [mp setFullscreen:YES];
    [[mp view] setFrame:CGRectMake(0, 0, PMW ,PMH)];
    [self upPlayViewFrame];
    [[self view] addSubview:[mp view]];
    [self.view addSubview:loadingAni];
    [mp prepareToPlay];
    [mp play];
    
}





- (void) moviePlayerLoadStateChanged:(NSNotification*)notification
{
    
    [loadingAni stopAnimating];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self upPlayViewFrame];
    
    // Unless state is unknown, start playback
    if ([mp loadState] != MPMovieLoadStateUnknown)
    {
        // Remove observer
        [[NSNotificationCenter     defaultCenter] removeObserver:self
                                                            name:MPMoviePlayerLoadStateDidChangeNotification                                                  object:nil];
    }
}


- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    //[[UIApplication sharedApplication] setStatusBarHidden:YES];
    //还原状态栏为默认状态
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    // Remove observer
    [[NSNotificationCenter     defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}




-(void)changeFrames:(NSNotification *)notification
{
    [self upPlayViewFrame];
}




-(void)upPlayViewFrame{


    mp.controlStyle = MPMovieControlStyleNone;
    
    CGRect buf = mp.view.frame;
    int dd = [[UIDevice currentDevice] orientation];
    if (dd==UIDeviceOrientationLandscapeLeft) {//按钮左边
        buf = mp.view.frame;
        buf.size.width = PMW;
        buf.size.height = PMH;
        [mp.view setCenter:CGPointMake(buf.size.width/2, buf.size.height/2)];
    }else if(dd==UIDeviceOrientationLandscapeRight){
        
        buf = mp.view.frame;
        buf.size.width = PMW;
        buf.size.height = PMH;
        [mp.view setCenter:CGPointMake(buf.size.width/2, buf.size.height/2)];
    }else if (dd==UIDeviceOrientationPortrait){
        buf = mp.view.frame;
        buf.size.width = PMW;
        buf.size.height = PMH;
        [mp.view setCenter:CGPointMake(buf.size.width/2, buf.size.height/2)];
    }
    
    [mp.view setFrame:buf];
    mp.controlStyle = MPMovieControlStyleFullscreen;
}





-(void)btnOnClik:(UIButton*)sender{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


- (BOOL)prefersStatusBarHidden {
    return true;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationFade;
}



-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    [AppDelegate sharedAppDelegate].isPlayMov = false;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [AppDelegate sharedAppDelegate].isPlayMov = true;
}


@end